################################################################################
# Judicial Ideology as Text -- Figure 3: Static Compared to MQ
# Truscott & Romano
# Updated February 2025
################################################################################


################################################################################
# Load Source
################################################################################

source('R/truscott_romano_source.R') # Load Source


################################################################################
# Figure 3: Static Ideal v. Martin-Quinn (Z-Score Normalized/Standardized)
################################################################################

{

  correlation <- mq %>%
    dplyr::select(term, justiceName, post_mn, post_025, post_975) %>%
    filter(term >= 2005) %>%
    filter(term <= 2021) %>%
    mutate(justiceName = sub(".*([A-Z])", "\\1", justiceName)) %>%
    mutate(justiceName = ifelse(justiceName == 'Connor', "O'Connor", justiceName)) %>%
    group_by(justiceName) %>%
    summarise(mean = mean(post_mn)) %>%
    left_join(wordshoals_static$High$summary_output %>%
                dplyr::select(author, theta) %>%
                rename(justiceName = author),
              by = 'justiceName') %>%
    mutate(justiceName = ifelse(justiceName == 'Connor', "O'Connor", justiceName),
           ideology = ifelse(justiceName %in% c('Ginsburg', 'Sotomayor', 'Breyer', 'Kagan'), 'Democratic Appointee', 'Republican Appointee')) %>%
    rename(mean_mq = mean,
           theta = theta,
           justice = justiceName)  %>%
    mutate(
      theta_scaled = (theta - mean(theta)) / sd(theta),
      mean_mq_scaled = (mean_mq - mean(mean_mq)) / sd(mean_mq),
      theta_scaled = abs(theta_scaled),
      mean_mq_scaled = abs(mean_mq_scaled))

  message('Correlation (Normalized): ', cor(correlation$theta_scaled, correlation$mean_mq_scaled, method = 'pearson'))

  message('Correlation (Not Normalized): ', cor(correlation$theta, correlation$mean_mq, method = 'pearson'))

  figure_3 <-  mq %>%
    dplyr::select(term, justiceName, post_mn, post_025, post_975) %>%
    filter(term >= 2005) %>%
    filter(term <= 2021) %>%
    mutate(justiceName = sub(".*([A-Z])", "\\1", justiceName)) %>%
    mutate(justiceName = ifelse(justiceName == 'Connor', "O'Connor", justiceName)) %>%
    group_by(justiceName) %>%
    summarise(mean = mean(post_mn)) %>%
    left_join(wordshoals_static$High$summary_output %>%
                dplyr::select(author, theta) %>%
                rename(justiceName = author),
              by = 'justiceName') %>%
    mutate(justiceName = ifelse(justiceName == 'Connor', "O'Connor", justiceName),
           ideology = ifelse(justiceName %in% c('Ginsburg', 'Sotomayor', 'Breyer', 'Kagan'), 'Democratic Appointee', 'Republican Appointee')) %>%
    rename(mean_mq = mean,
           mean_theta = theta,
           justice = justiceName)  %>%
    mutate(
      mean_theta_scaled = (mean_theta - mean(mean_theta)) / sd(mean_theta),
      mean_mq_scaled = (mean_mq - mean(mean_mq)) / sd(mean_mq),
      mean_theta_scaled = abs(mean_theta_scaled),
      mean_mq_scaled = abs(mean_mq_scaled))  %>%
    ggplot(aes(x = mean_theta_scaled, y = mean_mq_scaled)) +
    geom_segment(x = 0, xend = 2, y = 0, yend = 2, size = 1.25) +
    geom_point(aes(fill = ideology), colour = 'black', shape = 21, size = 3) +
    scale_fill_manual(values = c('deepskyblue3', 'red3')) +
    geom_vline(xintercept = 0, linetype = 2, alpha = 1/3) +
    geom_hline(yintercept = 0, linetype = 2, alpha = 1/3) +
    labs(x = '\nWordshoal (Scaled)',
         y = 'Martin-Quinn (Scaled)\n') +
    scale_y_continuous(breaks = seq(0.25, 1.5, 0.25), lim = c(0, 1.75), expand = c(0, 0)) +
    scale_x_continuous(breaks = seq(0.25, 1.5, 0.25), lim = c(0, 1.75), expand = c(0, 0)) +
    ggrepel::geom_label_repel(aes(label = justice), label.padding = 0.25) +
    ggplot2::annotate('label', x = 0.25, y = 1.5, label = 'More Ideological\n(Martin-Quinn)', size = 5) +
    ggplot2::annotate('label', x = 1.5, y = 0.25, label = 'More Ideological\n(Static Wordshoal)', size = 5) +
    theme_minimal() +
    theme(panel.border = element_rect(size = 1, colour = 'black', fill = NA),
          axis.text = element_text(size = 12, colour = 'black'),
          axis.title = element_text(size = 14, colour = 'black'),
          legend.position = 'bottom',
          legend.text = element_text(size = 14, colour = 'black'),
          legend.box.background = element_rect(size = 1, colour = 'black', fill = NA),
          legend.title = element_blank())

  ggsave(figure_3,
         file = 'figures/Figure_3.png',
         width = 8,
         height = 8,
         units = 'in',
         bg = 'white')

  ggsave('figures_tiff/Figure_3.tiff',
         plot = figure_3,
         device = 'tiff',
         dpi = 600,
         width = 10,
         height = 10,
         units = 'in',
         bg = 'white')

} # Figure 3: Static Ideal v. Martin-Quinn (Z-Score Normalized/Standardized)
